import java.io.File
import java.util.zip.ZipOutputStream
import java.util.zip.CRC32
import java.io.FileOutputStream
import java.util.zip.CheckedOutputStream
import java.util.zip.ZipEntry


object ZipUtils {
    fun compressSubDir(srcPath: String, destPath: String) {
        val cos = CheckedOutputStream(FileOutputStream(File(destPath)), CRC32())
        val zos = ZipOutputStream(cos)
        val srcFile = File(srcPath)
        if (srcFile.isDirectory) {
            srcFile.listFiles().forEach {
                compress(it, zos, "")
            }
        }
        zos.close()
        cos.close()
    }

    fun compress(file: File, zos: ZipOutputStream, baseDir: String) {
        if (file.isDirectory) {
            val filePath = baseDir + file.name + File.separator
            zos.putNextEntry(ZipEntry(filePath))
            file.listFiles().forEach {
                compress(it, zos, filePath)
            }
        } else {
            if (!file.name.equals(".DS_Store")) {
                zos.putNextEntry(ZipEntry(baseDir + file.name))
                zos.write(file.readBytes())
            }
        }
    }
}
